package com.yanshaohui.springboot.controller

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

import com.yanshaohui.springboot.entity.AddressEntity
import com.yanshaohui.springboot.service.AddressService

import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiOperation

@RestController
class AddressController {

	@Autowired
	AddressService addressService;
	
	@ApiOperation(value="创建地址")
	@ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PostMapping("/address")
	def add(@RequestBody AddressEntity address){
		addressService.insert(address);
	}

	@ApiOperation(value="更新地址")
	@ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PutMapping("/address")
	def update(@RequestBody AddressEntity address){
		addressService.update(address);
		"success";
	}
	
	@ApiOperation(value="获取地址详细信息", notes="根据地址名来获取地址详细信息")
	@ApiImplicitParam(name = "name", value = "地址", paramType="path",required = true, dataType = "String")
	@GetMapping("/address/{name}")
	def findAddress(@PathVariable("name") String name){
		addressService.findByName(name);
	}
	
	@ApiOperation(value="获取单项地址信息", notes="根据地址名来获取单项地址信息")
	@ApiImplicitParam(name = "name", value = "地址", paramType="path",required = true, dataType = "String")
	@GetMapping("/address/part/{name}")
	def findAddressPart(@PathVariable("name") String name){
		addressService.findAddressByName(name)
	}
	
	@ApiOperation(value="删除地址（ByName）")
	@ApiImplicitParam(name = "name", value = "Name", paramType="path",required = true, dataType = "String")
	@DeleteMapping("/address/name/{name}")
	def deleteAddressName(@PathVariable("name") String name){
		addressService.deleteByName(name);
		"success";
	}
	
	@ApiOperation(value="删除地址（ById）")
	@ApiImplicitParam(name = "id", value = "Id", paramType="path",required = true, dataType = "Long")
	@DeleteMapping("/address/{id}")
	def deleteAddress(@PathVariable("id") Long id){
		addressService.delete(id);
		"success";
	}
	
	@ApiOperation(value="获取地址详细信息(属性可为空)", notes="根据地址名来获取地址详细信息")
	@GetMapping("/dyna/address")
	def findAddress(AddressEntity entity){
		addressService.dynaFind(entity);
	}
	
	@ApiOperation(value="创建地址(属性可为空)")
	@ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PostMapping("/dyna/address")
	def dynaAdd(@RequestBody AddressEntity address){
		addressService.dynaInsert(address);
	}
	
	@ApiOperation(value="更新地址(属性可为空)")
	@ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PutMapping("/dyna/address")
	def dynaUpdate(@RequestBody AddressEntity address){
		addressService.dynaUpdate(address);
		"success";
	}
}
